package com.forhappy.light3ds8.ui;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.forhappy.light3ds8.R;
import com.forhappy.light3ds8.shareprefrence.Config;

public class Pg3_project_settings extends LinearLayout
		implements View.OnClickListener, TextWatcher{
	private static final String TAG = "Pg3_project_settings";

	Button bSaveSettings = null;
	public Pg3_project_settings(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.d(TAG, "enter Pg2_send_to_arm()");
	}

	@Override
	protected void onFinishInflate() {
		Log.d(TAG, "enter onFinishInflate()");
		super.onFinishInflate();
	}

	@Override
	protected void onAttachedToWindow() {
		Log.d(TAG, "enter onAttachedToWindow()");
		super.onAttachedToWindow();
		bSaveSettings = (Button) findViewById(R.id.bSaveSettings);
		bSaveSettings.setOnClickListener(this);

		EditText eUserName = (EditText) findViewById(R.id.eUserName);
		EditText ePassword = (EditText) findViewById(R.id.ePassword);

		Pair<String, String>nameAndPwd = Config.getUserNamePassword(getContext());
		eUserName.setText(nameAndPwd.first);
		ePassword.setText(nameAndPwd.second);

		CheckBox cNoNeedSeeOthers = (CheckBox) findViewById(R.id.cNoNeedSeeOthers);
		cNoNeedSeeOthers.setChecked(Config.getNoNeedSeeOthers(getContext()));
//		eUserName.addTextChangedListener(this);
//		ePassword.addTextChangedListener(this);
		bSaveSettings.setEnabled(false);
	}

	@Override
	protected void onDetachedFromWindow() {
		Log.d(TAG, "enter onDetachedFromWindow()");
		super.onDetachedFromWindow();
	}
	@Override
	public void onClick(View view) {
		Log.d(TAG, "enter onClick(view:" + view + ")");
		if(view.getId() == R.id.bSaveSettings){
			EditText eUserName = (EditText) findViewById(R.id.eUserName);
			EditText ePassword = (EditText) findViewById(R.id.ePassword);
			String strUserName = eUserName.getText().toString();
			String strPassword = ePassword.getText().toString();

			Config.setUserNamePassword(getContext(), strUserName, strPassword);

			CheckBox noNeedSeeOthers = (CheckBox) findViewById(R.id.cNoNeedSeeOthers);
			Config.setNoNeedSeeOthers(getContext(), noNeedSeeOthers.isChecked());
			bSaveSettings.setEnabled(false);
			Toast.makeText(getContext(), "save config over", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void afterTextChanged(Editable arg0) {
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		bSaveSettings.setEnabled(true);
	}



}
